package 排序;

import java.util.Arrays;

public class 快速排序 {
    private static void sort(int[] ints, int left, int right) {
        if (left >= right) return;
        int index = left; // 选取第一个元素为基准元素
        int value = ints[left]; // 拿去最后一个元素的值
        ints[left] = ints[right - 1]; // 将最后一个元素提到前面，让其好找出位置
        ints[right - 1] = value;
        for (int i = left; i < right - 1; i++) {
            if (ints[i] < value) { // Jiaohuan
                int t = ints[i];
                ints[i] = ints[index];
                ints[index] = t;
                index++;
            }
        }
        ints[right - 1] = ints[index];
        ints[index] = value;
        sort(ints, left, index - 1);
        sort(ints, index + 1, right);
    }

    public static void main(String[] args) {
        int[] ints = {3, 4, 51, 2, 5, 4576, 3, 4, 234, 7, 89};
        sort(ints, 0, ints.length);
        System.out.println(Arrays.toString(ints));
    }
}
